// 首先安装 sudo apt install openssl libssl-dev

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <openssl/evp.h>

// 函数用于计算并输出 MD5 哈希值
void calculate_md5(const char *input)
{
    EVP_MD_CTX *md5Context = EVP_MD_CTX_new();
    const EVP_MD *md5 = EVP_md5();
    unsigned char digest[EVP_MAX_MD_SIZE];
    unsigned int digestLength;

    if (md5Context == nullptr || md5 == nullptr) {
        std::cerr << "Failed to initialize MD5 context." << std::endl;
        return;
    }

    if (EVP_DigestInit_ex(md5Context, md5, nullptr) != 1) {
        std::cerr << "Failed to initialize MD5 digest." << std::endl;
        EVP_MD_CTX_free(md5Context);
        return;
    }

    if (EVP_DigestUpdate(md5Context, input, strlen(input)) != 1) {
        std::cerr << "Failed to update MD5 digest." << std::endl;
        EVP_MD_CTX_free(md5Context);
        return;
    }

    if (EVP_DigestFinal_ex(md5Context, digest, &digestLength) != 1) {
        std::cerr << "Failed to finalize MD5 digest." << std::endl;
        EVP_MD_CTX_free(md5Context);
        return;
    }

    EVP_MD_CTX_free(md5Context);

    std::ostringstream oss;
    for (unsigned int i = 0; i < digestLength; i++) {
        oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(digest[i]);
    }
    std::cout << oss.str() << std::endl;
}